package model

import (
	"gorm.io/gorm"
)

type Favorite struct {
	Model
	Name   string `json:"name,omitempty"`
	Desc   string `json:"desc,omitempty"`
	UserID uint   `json:"user_id,omitempty"`
}

func (f Favorite) TableName() string {
	return "img_favorite"
}

func (f Favorite) Create(db *gorm.DB) error {
	return db.Create(&f).Error
}

func (f Favorite) Update(db *gorm.DB) error {
	return db.Model(&f).Updates(&f).Error
}

func (f Favorite) Get(db *gorm.DB) (*Favorite, error) {
	var favorite Favorite
	err := db.Where(&f).First(&favorite).Error
	return &favorite, err
}

func (f Favorite) List(db *gorm.DB, pageOffset int, pageSize int) ([]*Favorite, error) {
	var favorites []*Favorite
	err := db.Where(&f).Offset(pageOffset).Limit(pageSize).Find(&favorites).Error
	return favorites, err
}

func (f Favorite) Delete(db *gorm.DB) error {
	return db.Delete(&f).Error
}

func (f Favorite) Count(db *gorm.DB) (int, error) {
	var count int64
	err := db.Model(&f).Where(&f).Count(&count).Error
	return int(count), err
}
